<?php

namespace Home\Model;

use Think\Model;

class CourierCodeModel extends Model {

    protected $tableName = 'courier_num';

    function getList($where = array()) {
        $page_size = 10;
        $count = $this->where($where)->count();
        $page = new \Think\Page($count, $page_size);
        $list = $this->where($where)->order('add_time desc')->limit($page->firstRow, $page_size)->select();
        $p = I('get.p', 1, 'intval');
        $html = page($p, $page->totalRows, $page_size, 8);
        return array('list' => $list, 'page' => $html, 'star' => $page->firstRow);
    }

    function getState() {
        $where = array('status' => 0);
        $result = $this->where($where)->group('state')->field('state')->select();
        return $result;
    }

    function getCategory() {
        $where = array('status' => 0);
        $result = $this->where($where)->group('category')->field('category')->select();
        return $result;
    }

    function getCourierInfo($id) {
        return $this->find($id);
    }

    function addCourier($data) {
        return M('order')->addAll($data);
    }

    function modifyCourierStatus($ids) {
        $where = array('id' => array('IN', "{$ids}"));
        return $this->where($where)->setField('status', 1);
    }

    function getOrderNum($user_id) {
        $star_time = strtotime(date('Y-m-d'));
        $ent_time = strtotime(date('Y-m-d') . ' 23:59:59');
        $where = array('user_id' => $user_id, 'type' => 1);
        $where['add_time'] = array('between', array($star_time, $ent_time));
        return M('order')->where($where)->count('id');
    }

    function getUserIndo($user_id) {
        return M('user')->find($user_id);
    }

    function modifyUserAmount($user_id, $amount) {
        $where = array('id' => $user_id);
        return M('user')->where($where)->setField('amount', $amount);
    }

    function getMyCourierCodeList($where) {
        $page_size = 10;
        $count = M('order')->where($where)->count();
        $page = new \Think\Page($count, $page_size);
        $list = M('order')->where($where)->order('add_time desc')->limit($page->firstRow, $page_size)->select();
        $p = I('get.p', 1, 'intval');
        $html = page($p, $page->totalRows, $page_size, 8);
        return array('list' => $list, 'page' => $html, 'star' => $page->firstRow);
    }

    function getCourierList($ids) {
        $where = array('id' => array('IN', "{$ids}"));
        return $this->where($where)->select();
    }

    function FreeCourierCodeDel($ids) {
        $where = array('id' => array('IN', "{$ids}"));
        return M('order')->where($where)->setField('is_deleted', 0);
    }

    function getExportList($user_id) {
        $where = array('user_id' => $user_id, array('is_deleted' => 1));
        return M('order')->where($where)->order('add_time desc')->select();
    }

}
